package com.example.myapplication;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import java.util.ArrayList;

public class FragmentStatePagerAdapter extends androidx.fragment.app.FragmentStatePagerAdapter {
  private ArrayList<Fragment> fragments;

  private Context context;

  public FragmentStatePagerAdapter(Context context,FragmentManager fragmentManager, ArrayList<Fragment> data) {
    super(fragmentManager,BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
    fragments=data;
    this.context=context;
  }



  @NonNull @Override public Fragment getItem(int position) {
    return fragments.get(position);
  }

  @Override public int getCount() {
    return fragments.size();
  }

  @Nullable @Override public CharSequence getPageTitle(int position) {
    SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
    //返回每个Tab的标题
    if (position == 0) {
      //Drawable icon = selectedPosition == 0 ?
      //    context.getDrawable(R.drawable.notebook_selected) :
      //    context.getDrawable(R.drawable.notebook_unselect);

      //if (selectedPosition == position) {
      //  icon = context.getDrawable(R.drawable.notebook_selected);
      //} else {
      //
      //}
      Drawable icon = context.getDrawable(R.drawable.notebook_unselect);

      icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
      spannableStringBuilder.append(" ");
      ImageSpan imageSpan = new ImageSpan(icon, ImageSpan.ALIGN_BOTTOM);

      spannableStringBuilder.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
      //添加换行符
      spannableStringBuilder.append("\n");
      //添加说明文字
      //int color = selectedPosition == 0 ? context.getColor(R.color.selected_color)
      //    : context.getColor(R.color.grey);
      int color = context.getColor(R.color.grey);
      ForegroundColorSpan colorSpan = new ForegroundColorSpan(color);


      spannableStringBuilder.append("笔记");
      spannableStringBuilder.setSpan(colorSpan, spannableStringBuilder.length() - 2,
          spannableStringBuilder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
      //if (selectedPosition == position) {
      //  spannableStringBuilder.setSpan(
      //      new ForegroundColorSpan(context.getColor(R.color.selected_color)), 2, 4,
      //      Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
      //}
    } else if (position == 1) {
      //Drawable icon = selectedPosition == 1 ?
      //    context.getDrawable(R.drawable.cat_user_selected) :
      //    context.getDrawable(R.drawable.cat_user_unselect);

      //if (selectedPosition == position) {
      //  icon = context.getDrawable(R.drawable.cat_user_selected);
      //} else {
      //  icon = context.getDrawable(R.drawable.cat_user_unselect);
      //}
      Drawable icon = context.getDrawable(R.drawable.cat_user_unselect);
      icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
      spannableStringBuilder.append(" ");
      ImageSpan imageSpan = new ImageSpan(icon, ImageSpan.ALIGN_BOTTOM);

      spannableStringBuilder.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
      spannableStringBuilder.append("\n");

      //int color = selectedPosition == 0 ? context.getColor(R.color.selected_color)
      //    : context.getColor(R.color.grey);
      int color = context.getColor(R.color.grey);
      ForegroundColorSpan colorSpan = new ForegroundColorSpan(color);

      spannableStringBuilder.append("主页");
      //if (selectedPosition == position) {
      //  spannableStringBuilder.setSpan(
      //      new ForegroundColorSpan(context.getColor(R.color.selected_color)), 2, 4,
      //      Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
      //}
      spannableStringBuilder.setSpan(colorSpan, spannableStringBuilder.length() - 2,
          spannableStringBuilder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    } else {
      return "未定义页面";
    }
    return spannableStringBuilder;
  }


}
